<template>
  <div>
    <el-button
      type="primary"
      v-has-permission='`Spu-add`'
      class="el-icon-plus"
      :disabled="!category3Id"
      @click="$emit('updateIsShow', 2)"
    >
      添加SPU属性
    </el-button>

    <el-table :data="spuList" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column label="SPU名称" prop="spuName"> </el-table-column>
      <el-table-column label="SPU描述" prop="description"> </el-table-column>
      <el-table-column label="操作" width="300px">
        <template v-slot="{ row, $index }">
          <el-tooltip content="添加SKU" placement="top">
            <el-button
              type="primary"
              class="el-icon-plus"
              size="mini"
              v-has-permission='`Spu-add`'
              @click="addSKU(row.id)"
            ></el-button>
          </el-tooltip>

          <el-tooltip content="修改SPU" placement="top">
            <el-button
              type="warning"
              class="el-icon-edit"
              size="mini"
              @click="updateSpu(row)"
            ></el-button>
          </el-tooltip>

          <el-tooltip content="添加SPU" placement="top">
            <el-button
              type="primary"
              class="el-icon-plus"
              size="mini"
              @click="addSpu(row)"
            ></el-button>
          </el-tooltip>

          <el-tooltip content="查看SPU" placement="top">
            <el-button
              type="info"
              class="el-icon-info"
              size="mini"
              @click="lookSpu(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除SPU" placement="top">
            <el-button
              type="danger"
              class="el-icon-delete"
              size="mini"
              @click="delSpu(row, $index)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 6, 9, 12]"
      :page-size="limit"
      layout="prev, pager, next, jumper,total,sizes"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { reqSpuInfoList, reqDeleteSpu } from "@/api/production/spu";
export default {
  name: "SpuList",
  data() {
    return {
      spuList: [],
      page: 1,
      limit: 3,
      total: 0,
      loading: false,
    };
  },
  computed: {
    ...mapState("category", ["category1Id", "category2Id", "category3Id"]),
  },
  methods: {
    ...mapMutations("spu", ["SET_SPU_ID", "SET_OPERATOR"]),
    handleSizeChange(val) {
      this.limit = val;
      this.getSpuInfoList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSpuInfoList();
    },
    // 得到spuList
    async getSpuInfoList() {
      this.loading = true;
      let { page, limit, category3Id } = this;
      let res = await reqSpuInfoList({
        page,
        limit,
        category3Id,
      });
      this.loading = false;
      this.spuList = res.records;
      this.total = res.total;
    },
    // 添加sku，页面跳转
    addSKU(spuId) {
      // 兄弟之间通讯需要spuId传到Vuex
      // console.log(spuId);1602
      // 修改
      this.SET_SPU_ID(spuId);
      this.$emit("updateIsShow", 3);
    },
    // 删除spu
    async delSpu(row, index) {
      await reqDeleteSpu(row.id);
      this.spuList.splice(index, 1);
      this.$message({
        type: "success",
        message: "删除成功",
      });
    },
    // 查看spu
    lookSpu(row) {
      //传spuId,和要触发展示数据
      this.SET_SPU_ID(row.id);
      this.SET_OPERATOR("lookSpu");
      this.$emit("updateIsShow", 2);
    },
    // 修改spu
    updateSpu(row) {
      this.SET_SPU_ID(row.id);
      this.SET_OPERATOR("updateSpu");//告诉当前的操作
      this.$emit("updateIsShow", 2);//控制显示什么页面
    },
    // 添加spu
    addSpu(row){
      this.$emit("updateIsShow", 2);
    }
  },
  watch: {
    category3Id: {
      handler(val) {
        if (!val) {
          // 清空列表
          this.spuList = [];
          this.total = 0;
          return;
        }
        this.getSpuInfoList();
      },
      immediate: true,
    },
  },
};
</script>

<style>
</style>